<script lang="jsx" setup>
import { ref } from 'vue';
import { Button } from 'ant-design-vue';
import { deleteItem, getList } from '@/service/api/modules/workwear/mold-management';

const props = defineProps({
  moldId: {
    type: String,
    default: null
  }
});

const columns = [
  {
    key: 'dataIndex',
    dataIndex: 'dataIndex',
    title: '序号',
    width: 70,
    fixed: 'left',
    align: 'center',
    checked: true,
    ellipsis: true
  },
  {
    key: 'lotNo',
    dataIndex: 'lotNo',
    title: 'lot号',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 120
  },
  {
    key: 'dateTime',
    dataIndex: 'dateTime',
    title: '日期',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 120
  },
  {
    key: 'punchNum',
    dataIndex: 'punchNum',
    title: '当日冲次',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 100
  },
  {
    key: 'punchTotal',
    dataIndex: 'punchTotal',
    title: '累计冲次',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 100
  },
  {
    key: 'grindNum',
    dataIndex: 'grindNum',
    title: '研磨次数',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 100
  },
  {
    key: 'grindPunchTotal',
    dataIndex: 'grindPunchTotal',
    title: '研磨后累计冲次',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 100
  },
  {
    key: 'grindFlagName',
    dataIndex: 'grindFlagName',
    title: '是否研磨',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 100
  },
  {
    key: 'createBy',
    dataIndex: 'createBy',
    title: '创建人',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 100
  },
  {
    key: 'operator',
    dataIndex: 'operator',
    title: '作业员',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 100
  },
  {
    key: 'remark',
    dataIndex: 'remark',
    title: '备注',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 100
  },
  {
    key: 'table-cz',
    dataIndex: 'table-cz',
    title: '操作',
    align: 'center',
    checked: true,
    ellipsis: true,
    width: 100,
    fixed: 'right',
    customRender: ({ record }) => {
      return (
        <div>
          <Button
            type="link"
            danger
            class="pl-3px pr-3px"
            v-hasAuth={[['stamping-die-management:mold-management:update']]}
            onClick={() => {
              del(record);
            }}
          >
            删除
          </Button>
        </div>
      );
    }
  }
];
const tableData = ref([]);
const loading = ref(false);

function getTableData() {
  loading.value = true;
  getList({ moldId: props.moldId })
    .then(({ data, error }) => {
      data.forEach((item, index) => {
        item.dataIndex = index + 1;
      });
      tableData.value = data;
    })
    .finally(() => (loading.value = false));
}

getTableData();

function del(row) {
  window.$modal.confirm({
    title: `确定删除?`,
    onOk: () => {
      deleteItem(row.moldUseRecordUuid).then(({ error }) => {
        if (!error) {
          window.$message.success('删除成功');
          getTableData();
        }
      });
    },
    onCancel: () => {}
  });
}
</script>

<template>
  <ATable
    :columns="columns"
    :data-source="tableData"
    row-key="moldUseRecordUuid"
    :scroll="{ x: 1800, y: 'auto' }"
    :loading="loading"
    :pagination="false"
    class="h-full"
    bordered
    size="small"
  />
</template>

<style lang="scss" scoped></style>
